/*  About sequence list
    some test and practice
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAXLEN 40

//sequence list:we malloc array, and index them with array way
/*
    have a sequence list:add, delete, change, find(index), clear, destroy
*/

typedef struct{
    int *Elem;
    int len;//list length
    // int b;
}Sqlist;

// void getLonger(Node *array);

void CreateList(Sqlist *L, int n) {
    //init
    L->Elem = (int*)malloc(n * sizeof(int));
    printf("Input data(space split):\n");//scan n numbers, if scan more, cast it.
    for(int i = 0; i < n; i++) {
        scanf("%d", &L->Elem[i]);
    }
    L->len = n;//length is n
    printf("\nCreate success!\n");
}

void Insert_sq(Sqlist *L, int index, int elem) {
    //sqlen is sqlist length, ensure in it.
    if(index < 0 || index > L->len + 1) {
        //try bound, catch exception!
        printf("index overflow!\n");
        return;
    }
    if(L->len >= MAXLEN) {
        printf("List is maxsize! Can't insert more.\n");
        return;
    }
    //insert
    for(int i = L->len; i >= index; i--) {
        L->Elem[i+1] = L->Elem[i];
    }
    L->Elem[index] = elem;
    L->len++;
}

void Del_sq(Sqlist *L, int index) {
    if(L->len == 0) {
        printf("List is Empty!\n");
    }
    if(index < 0 || index > L->len+1) {
        printf("Index Error!\n");
        return;
    }
    //remove
    for(int i = index; i < L->len; i++) {
        L->Elem[i] = L->Elem[i+1];
    }
    L->len--;
    printf("\nremove success!\n");
}

void printlist_sq(Sqlist L) {
    //we don't change it.
    printf("list is below:\n");
    for(int i = 0; i < L.len; i++) {
        printf("%d ", L.Elem[i]);
    }
}

int main(int argc, char const *argv[])
{
    Sqlist L;
    int n;
    printf("input n:\n");
    scanf("%d", &n);

    CreateList(&L, n);
    printlist_sq(L);

    printf("\nnow insert '8', index 0:\n");
    Insert_sq(&L, 0, 8);
    printlist_sq(L);

    printf("\nnow remove index 0");
    Del_sq(&L, 0);
    printlist_sq(L);
    // Node *array;
    // Node buff1;
    // array = (Node*)malloc(5 * sizeof(Node));

    // array[0].a = 5;
    // array[0].b = 6;

    // for(int i = 1; i < 5; i++) {
    //     array[i].a = i+3;
    //     array[i].b = i+8;
    // }

    // for(int i = 0; i < 5; i++) {
    //     printf("array[%d].a = %d\n", i, array[i].a);
    //     printf("array[%d].b = %d\n", i, array[i].b);
    // }

    // printf("Now we make it longer:\n");
    // //getLonger(array);
    // for(int i = 0; i < 10; i++) {
    //     printf("array[%d].a = %d\n", i, array[i].a);
    //     printf("array[%d].b = %d\n", i, array[i].b);
    // }


    return 0;
}

//practice "realloc"
// void getLonger(Node *array) {
//     array = (Node*)realloc(array, 10*sizeof(Node));//realloc to 10 node
//     array[8].a = 88;
//     array[8].b = 99;
// }
